import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user',
  () => {
    const userInfo = ref(null)
    const token = ref('')
    const city = ref('')
    const userId = ref('') // 新增 userId

    function updateUserInfo(info) {
      userInfo.value = info
    }

    function changeCity(newCity) {
      city.value = newCity
    }

    const setToken = (newToken) => {
      token.value = newToken
    }

    // 新增设置userId的方法
    const setUserId = (newUserId) => {
      userId.value = newUserId
    }

    const clearUserInfo = () => {
      userInfo.value = null
      token.value = ''
      city.value = ''
      userId.value = '' // 新增 userId
    }
// 123
    return {
      userInfo,
      token,
      city,
      userId, // 导出 userId
      updateUserInfo,
      changeCity,
      setToken,
      setUserId, // 导出设置方法
      clearUserInfo
    }
  },
  {
    persist: true // 持久化所有状态
  }
)